#include "Label.h"


CLabel* CLabel::NewL(const TRect& aRect,const CCoeControl* aParent)
	{
	CLabel* self = CLabel::NewLC(aRect,aParent);
    CleanupStack::Pop(self);
    return self;
	}

CLabel* CLabel::NewLC(const TRect& aRect,const CCoeControl* aParent)
	{
	CLabel* self = new(ELeave) CLabel();
    CleanupStack::PushL(self);
    self->ConstructL(aRect,aParent);
    return self;
	}

CLabel::CLabel()
	{
	iControlType = ELabel;
	iText = NULL;
	iTextColor = 0;
	iUnderLine = EFalse;
	}

CLabel::~CLabel()
	{
	iInfoArray.Close();
	delete iText;
	iText = NULL;
	}

void CLabel::SetTextL(const TDesC& aText)
	{
	iTopLine = 0;
	iInfoArray.Close();
	if (!( iText && iText->Compare(aText) == 0 ))
		{
		delete iText;
		iText = NULL;
		iText = HBufC::NewL(aText.Length());
		iText->Des().Append(aText);
		}
	TInt loc, eloc, sloc, offset(0);
	TBool loop(ETrue);
	TLineInfo line;
	TInt idx(0);
	while (loop)
		{
		loc = calcLineChar(aText.Mid(offset), eloc, sloc);
		if (loc <= 0)
			{
			//iInfoArray->AppendL(aText.Mid(offset));
			line.iIndex = idx;
			line.iBegin = offset;
			line.iCount = eloc;
			iInfoArray.AppendL(line);
			break;
			}
		else
			{
			//iInfoArray->AppendL(aText.Mid(offset, eloc));
			line.iIndex = idx;
			line.iBegin = offset;
			line.iCount = eloc;
			iInfoArray.AppendL(line);
			offset += sloc;
			}
		idx++;
		}
	SetLayout();
	}

void CLabel::SetLayout()
	{
	CBaseControl::SetLayout();
	TRect rect = Rect();
	TInt height = iInfoArray.Count() * (iFont->HeightInPixels() + iLineSpace);
	height += 4;
	if (height < rect.Height())
		height = rect.Height();
	rect.SetHeight(height);
	SetRect(rect);
	}

void CLabel::ConstructL(const TRect& aRect,const CCoeControl* aParent)
	{
    if(aParent == NULL)
        {
        CreateWindowL();
        }
    else
        {
        SetContainerWindowL(*aParent);
        }

	iWordWrap = EFalse;

	iTopLine = 0;
	iLeftMargin = 8;
	iRightMargin = 8;
	iTopMargin = 2;
	iLineSpace = 1;
	iBorderWidth = aRect.Width();

	iFont = CEikonEnv::Static()->TitleFont();

    SetRect(aRect);

    ActivateL();
	}

void CLabel::SizeChanged(const TRect& aRect)
	{
	CBaseControl::SizeChanged(aRect);
//	if (iBorderWidth != aRect.Width())
//		{
//		iBorderWidth = aRect.Width();
//		SetRect(aRect);
//		if (iText)
//			{
//			SetTextL(*iText);
//			}
//		}
	}

void CLabel::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	CBaseControl::Draw(gc,aRect);
	TPoint position = Position();
	gc.DrawRect(aRect);
	gc.UseFont(iFont);
	if (iInfoArray.Count() > 0)
		{
		TUint y = position.iY + iFont->AscentInPixels() + iTopMargin;
		TInt count = iInfoArray.Count();

		for (TInt i = 0; i < count; i++, y += iFont->HeightInPixels() + iLineSpace)
			{
			gc.SetPenColor(iTextColor);
			gc.DrawText(iText->Mid(iInfoArray[i].iBegin, iInfoArray[i].iCount), TPoint(position.iX + iLeftMargin, y));
			if (iUnderLine)
                {
                gc.SetPenColor(KRgbGray);
                gc.DrawLine(TPoint(position.iX + iLeftMargin,y - 2),TPoint(aRect.Width() - iRightMargin,y - 2));
                }
			}
		}
	gc.DiscardFont();
	}

void CLabel::DrawFocusEffect(CWindowGc& /*gc*/, const TRect& /*aRect*/) const
	{
	
	}

TInt CLabel::calcLineChar(const TDesC& info, TInt& thisEnd, TInt& nextStart)
	{
	TInt len = -1;
	TInt wd = 0;
	for (TInt i = 0; i < info.Length(); i++)
		{
		if (info[i] == 0x0A)
			{
			thisEnd = len + 1;
			nextStart = len + 2;
			return len + 2;
			}
		else
			if (info[i] == 0x0D)
				{
				if (i + 1 < info.Length())
					{
					if (info[i + 1] == 0x0A)
						{
						thisEnd = len + 1;
						nextStart = len + 3;
						return len + 3;
						}
					else
						{
						thisEnd = len + 1;
						nextStart = len + 2;
						return len + 2;
						}
					}
				else
					{
					thisEnd = 0;
					nextStart = 0;
					return 0;
					}
				}

		wd = iFont->TextWidthInPixels(info.Left(i));
		if (wd > iBorderWidth - iLeftMargin - iRightMargin)
			{
			if (iWordWrap)
				{
				for (TInt j = len; j >= 0; j--)
					{
					if (info[j] < 0x30 || info[j] >= 128)
						{
						len = j + 1;
						break;
						}
					}
				}

			thisEnd = len;
			nextStart = len;
			return len;
			}

		len = i;
		}

	if (wd < iBorderWidth - iLeftMargin - iRightMargin)
		{
		thisEnd = len + 1;
		nextStart = len + 2;
		return -1;
		}
	else
		{
		thisEnd = 0;
		nextStart = 0;
		return 0;
		}
	}

void CLabel::SetTextColor(TInt aColor)
{
    iTextColor = aColor;
}

void CLabel::SetUnderLine(TBool aUnderLine)
{
    iUnderLine = aUnderLine;
}
